<?php
include($_SERVER['DOCUMENT_ROOT']."/functions/lists/html_colors.php");

define('TAG_START', htmlspecialchars('<x'));
define('TAG_END', htmlspecialchars('x<'));
define('TAG_ATTR_START', htmlspecialchars('<x=v<'));
define('TAG_ATTR_END', htmlspecialchars('x<'));

function parse($text) {
	$text = htmlspecialchars($text);
    $text = parse_gras($text);
    $text = parse_italique($text);
    $text = parse_souligne($text);
    $text = parse_barre($text);
    $text = parse_link($text);
    $text = parse_color($text);
    return $text;
}

function parse_gras($text) {
    $text = preg_replace('#' . str_replace('x', 'g', TAG_START) . '(.+)' . str_replace('x', 'g', TAG_END) . '#', '<strong>$1</strong>', $text);
    return $text;
}

function parse_italique($text) {
    $text = preg_replace('#' . str_replace('x', 'i', TAG_START) . '(.+)' . str_replace('x', 'i', TAG_END) . '#isU', '<em>$1</em>', $text);
    return $text;
}

function parse_souligne($text) {
    $text = preg_replace('#' . str_replace('x', 's', TAG_START) . '(.+)' . str_replace('x', 's', TAG_END) . '#isU', '<span style="text-decoration: underline">$1</span>', $text);
    return $text;
}

function parse_barre($text) {
    $text = preg_replace('#' . str_replace('x', 'b', TAG_START) . '(.+)' . str_replace('x', 'b', TAG_END) . '#isU', '<span style="text-decoration: line-through">$1</span>', $text);
    return $text;
}

function parse_link($text) {
    $balise_start = str_replace('x', 'l', TAG_ATTR_START);
    $balise_start = str_replace('v', '((s?ftp|https?)://[a-z0-9._/-]+)', $balise_start);

    $balise_end = str_replace('x', 'l', TAG_ATTR_END);

    $text = preg_replace('#' . $balise_start.'(.+)'.$balise_end.'#isU', '<a href="$1">$3</a>', $text);
    return $text;
}
	
function parse_color($text) {
    $balise_start = str_replace('x', 'c', TAG_ATTR_START);
    $balise_start = str_replace('v', '(('.html_colors('|').')|([0-9A-F]{6}))', $balise_start);

    $balise_end = str_replace('x', 'c', TAG_ATTR_END);

    $text = preg_replace('#' . $balise_start.'(.+)'.$balise_end.'#isU', '<span style="color: $1">$4</span>', $text);
    return $text;
}

?>